package com.google.code.jtasks.client.modules;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

/**
 * Модуль, отвечающий за конфигурацию клиента Jersey.
 */
public class JerseyClientModule extends AbstractModule {
    @Override
    protected void configure() {
        binder().requireExplicitBindings();
    }

    @Provides
    ClientConfig jerseyClientConfig() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JodaModule());
        mapper.registerModule(new GuavaModule());
        JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider(mapper);

        DefaultClientConfig config = new DefaultClientConfig();
        config.getSingletons().add(jacksonJsonProvider);

        return config;
    }

    @Provides @Singleton
    Client jerseyClient(ClientConfig clientConfig) {
        return Client.create(clientConfig);
    }
}

